package com.supermap.originTest2;

import com.supermap.data.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;


public class ExportPicture {


    public static void main(String[] args) {

        Workspace workspace = new Workspace();
        WorkspaceConnectionInfo workspaceConnectionInfo = new WorkspaceConnectionInfo("E:\\MapData\\202408\\图片导出\\WorkSpace.smwu");


        workspaceConnectionInfo.setType(WorkspaceType.SMWU);
        workspace.open(workspaceConnectionInfo);

        DatasourceConnectionInfo datasourceConnectionInfo = new DatasourceConnectionInfo();
        datasourceConnectionInfo.setEngineType(EngineType.UDBX);
        datasourceConnectionInfo.setServer("E:\\MapData\\202408\\图片导出\\DataSource.udbx");

        Datasource datasource = workspace.getDatasources().get(0);

        Dataset newDataset = null;
        DatasetVectorInfo datasetVectorInfo = new DatasetVectorInfo();
        datasetVectorInfo.setName("DrawCAD2");
        datasetVectorInfo.setType(DatasetType.CAD);
        newDataset = datasource.getDatasets().create(datasetVectorInfo);
        newDataset.setPrjCoordSys(PrjCoordSys.fromEPSG(4326));




        Recordset rc = ((DatasetVector) newDataset).getRecordset(false, CursorType.DYNAMIC);

//        rc.getBatch().getMaxRecordCount();
        // 创建一个 GeoPicture 对象，并设置其图片路径
        GeoPicture geoPicture = new GeoPicture();
        // 设置 GeoPicture 对象的位置属性
        Point2D point2D = new Point2D(0,0);
        geoPicture.setCenter(point2D);
        geoPicture.setHeight(10);
        geoPicture.setWidth(10);
        try {
            // 转为BufferedImage
            BufferedImage sourceImg = getImageFromUrl("http://gips2.baidu.com/it/u=195724436,3554684702&fm=3028&app=3028&f=JPEG&fmt=auto?w=1280&h=960");
            geoPicture.setImage(sourceImg);
            rc.addNew(geoPicture);
        } catch (Exception e) {
            System.err.println("[打印输出]-[绘制自定义图片失败]");
        }
        rc.update();

    }

    public static BufferedImage getImageFromUrl(String imageUrl) {
        BufferedImage image = null;
        try {
            URL url = new URL(imageUrl);
            image = ImageIO.read(url);
        } catch (IOException e) {
            e.printStackTrace(); // 处理异常
        }
        return image; // 返回获取到的图像（可能为 null）
    }

//    public static <HttpURLConnection> BufferedImage getBufferedImageDestUrl(String destUrl) {
//        HttpURLConnection conn = null;
//        BufferedImage image = null;
//        try {
//            URL url = new URL(destUrl);
//            conn = (HttpURLConnection) url.openConnection();
//            if (conn.getResponseCode() == 200) {
//                image = ImageIO.read(conn.getInputStream());
//                return image;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            conn.disconnect();
//        }
//        return image;
//
//    }
}
